package com.netas.kekmanager.services.serviceartifacts;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;

import com.netas.kekmanager.dao.UserDao;
import com.netas.kekmanager.dao.factory.DaoFactory;

@Service
public class KurumServiceImpl implements KurumService {
	
	private static final Logger logger = Logger
			.getLogger(KurumServiceImpl.class);
	

	/* (non-Javadoc)
	 * @see com.netas.kekmanager.services.serviceartifacts.KurumService#kurumSorgula(com.netas.kekmanager.services.serviceartifacts.KurumSorgulaRequest)
	 */
	@Override
	public KurumSorgulaResponse kurumSorgula(
			KurumSorgulaRequest kurumSorgulaRequest) {
		
		      KurumSorgulaResponse kurumResponse = new KurumSorgulaResponse();

      try {
         UserDao userDao = DaoFactory.createUserDao();
		 String md5pass = convertMd5(kurumSorgulaRequest.getPassword());
		 
         ArrayList<com.netas.kekmanager.dao.model.User> userList = (ArrayList<com.netas.kekmanager.dao.model.User>) userDao.findWhereUsernameEquals(kurumSorgulaRequest.getUsername());
         if (userList != null && (!userList.isEmpty())) {
            com.netas.kekmanager.dao.model.User user = userList.get(0);
			
            if (user.getPassword().equals(md5pass)) {
               User responseUser = new User();
               responseUser.setName(user.getName());
               responseUser.setAverageRating(user.getAverageRating().toString());
               responseUser.setEmail(user.getEmail());
               responseUser.setID(user.getId() + "");
               responseUser.setRegisterDate(user.getRegisterDate().toString());
               responseUser.setUsername(user.getUsername());
               responseUser.setUserTypeID(user.getUserTypeId() + "");
               kurumResponse.setUser(responseUser);
            } else {
               User responseUser = new User();
               kurumResponse.setUser(responseUser);
            }
         } else {
            User us = new User();
            kurumResponse.setUser(us);
         }
      }
      catch (Exception ex) {
         User us = new User();
         us.setName("ERROR-" + ex.getMessage()) ;
         kurumResponse.setUser(us);
      }

      return kurumResponse;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	 private static String convertMd5(String key) throws NoSuchAlgorithmException {

      MessageDigest md = MessageDigest.getInstance("MD5");
      md.reset();
      md.update(key.getBytes());
      byte[] digest = md.digest();
      BigInteger bigInt = new BigInteger(1, digest);
      String md5pass = bigInt.toString(16);
      while (md5pass.length() < 32) {
          md5pass = "0" + md5pass;
      }

      return md5pass;
  }


}
